Prevent Infinite Loops with a Valid Terminal Condition